<?php
class DynaGrid_Url {
	/**
	 * Array containing the url keys and values
	 *
	 * @var array
	 */
	private $parts;
	/**
	 * Constructor which separates the parts of an url
	 *
	 * @param string $url
	 */
	public function __construct($url) {
		$this->parts = array();		
		parse_str($url,$parts);		
		foreach((array)$parts as $key=>$value) {
			$this->parts[$key]=$value;
		}
	}
	public function __get($key) {
		if(array_key_exists($key,$this->parts)) {
			return $this->parts[$key];
		}
	}
	public function __set($key,$value) {
		$this->parts[$key] = $value;
	}
	public function __unset($key) {
		if(array_key_exists($key,$this->parts)){
			unset($this->parts[$key]);
		}
	}
	public function __isset($key) {
		return array_key_exists($key,$this->parts);
	}
	/**
	 * Draws and returns the url
	 *
	 * @version 0.91 (last-change: bug-fix with the last ampersand)
	 * @return string
	 */
	public function __toString() {
		reset($this->parts);
		$return = '?';
		
		$amount = count($this->parts)-1;		
		$i = 0;
			
		foreach($this->parts as $key=>$value) {
			if(!(bool)strlen($value)) { $i++; continue;	}
			
			if($i < $amount) { 
				$return .= $key.'='.$value.'&amp;';								
			} else {
				$return .= $key.'='.$value;
			}
			
			$i++;
		}
		return $return;
	}
	/**
	 * Returns a query you can use in your url
	 * @example ?key=value&key=value
	 *
	 * @return string
	 */
	public function getQueryString() {
		return $this->__toString();
	}
}